require "wondercap/lib/utils/logging"
require "wondercap/lib/deployment/model/area"

module ::Wondercap
module Deployment
module NameFilters
  
  class RemoteFreshOnlyFilter
    def call(names, base_conf)
      local_area = Area.find_by_name("local")
      raise "no current snapshot" unless local_area && local_area.current_snapshot

      remote_area = base_conf.area
      return names unless remote_area && remote_area.current_snapshot

      names.delete_if do |name|
        local_dep = local_area.current_snapshot.find_deployment_by_name(name)
        raise "no deployment for #{name} found" unless local_dep
        remote_dep = remote_area.current_snapshot.find_deployment_by_name(name)

        remote_dep.revision == local_dep.revision && remote_dep.deployed_at >= local_dep.deployed_at
      end
    end
  end
  
end
end
end

define_name_filter :remote_fresh_only, Wondercap::Deployment::NameFilters::RemoteFreshOnlyFilter.new
